package io.finer.erp.jeecg.stock.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.finer.erp.jeecg.stock.dto.StkEntryDetailDTO;
import io.finer.erp.jeecg.stock.dto.StkPurchaseOrderDTO;
import io.finer.erp.jeecg.stock.entity.StkIoBill;
import io.finer.erp.jeecg.stock.vo.FreightOrIoBillVo;
import io.finer.erp.jeecg.stock.vo.StkIoBillVO;

/**
 * @Description: 出入库单
 * @Author: jeecg-boot
 * @Date:   2020-03-31
 * @Version: V1.0
 */
public interface StkIoBillMapper extends BaseMapper<StkIoBill> {

    public IPage<StkIoBill> selectByStockIoType(IPage page, @Param("stockIoType") String stockIoType);
    
    @Select("SELECT * FROM stk_io_bill WHERE id IN (SELECT mid FROM stk_io_bill_entry WHERE material_id IN (SELECT id FROM bas_material WHERE code = #{code})) ORDER BY create_time DESC LIMIT 1")
    public StkIoBill selectByMaterialCode(@Param("code")String code);
    
    @Select("select * from stk_io_bill t where t.stock_io_type = '601' and RPAD(t.bill_proc_status,3,0)>=230 order by create_time desc limit 200")
    public List<StkIoBill> pdaGetPurchaseOrder();
    
    @Select("SELECT * FROM stk_io_bill t where t.id in (select mid from stk_io_bill_entry where material_id=#{materialId}) order by t.create_time DESC")
    public List<StkIoBill> getIoBillByMaterial(@Param("materialId")String materialId);
    
    @Select("SELECT * FROM stk_io_bill t where t.id in (select mid from stk_io_bill_entry where material_id=#{materialId}) order by t.create_time ASC")
    public List<StkIoBill> getIoBillByMaterialAsc(@Param("materialId")String materialId);

    @Select("SELECT * FROM stk_io_bill bill WHERE bill.id IN ( SELECT mid FROM stk_io_bill_entry WHERE material_id = (SELECT id FROM bas_material WHERE code = #{materialCode}) ) AND bill.stock_io_type = '201' ORDER BY create_time DESC ")
	public List<StkIoBill> getSaleOutBill(@Param("materialCode")String materialCode);

    @Select("SELECT * FROM stk_io_bill WHERE id IN (SELECT mid from stk_io_bill_entry WHERE material_id=#{materialId}) ORDER BY create_time DESC")
	public List<StkIoBill> getByMaterialId(String materialId);


    List<FreightOrIoBillVo> getBullBySell();

    List<FreightOrIoBillVo> getBullByBuy();

    StkIoBill getBillByBillNo(@Param("billNo") String billNo);

	public List<StkIoBill> getBillByMes(@Param("mesIds")List<String> mesIds, @Param("mesCodes")List<String> mesCodes);
	
	public List<StkPurchaseOrderDTO> getPurchaseOrderList(@Param("billNo")String billNo
			, @Param("beginDate")Date beginDate, @Param("endDate")Date endDate
			, @Param("supplierId")String supplierId, @Param("isApproved")String isApproved
			, @Param("isClosed")String isClosed);
	
	/*
	 * 采购单（供应商）列表汇总
	 */
	public List<StkPurchaseOrderDTO> querylistBySupplier(@Param("vo") StkIoBillVO vo);
	
	/**
	 * @Description 采购单（供应商）列表汇总详情
	 * @param vo
	 * @return
	 * Create at: 2021-08-25 17:18:54
	 * @author: xzs
	 * Revision:
	 *    2021-08-25 17:18:54 - first revision by xzs
	 *
	 */
	public List<StkEntryDetailDTO> queryEntryBySupplier(@Param("vo") StkIoBillVO vo);
	
	/**
	 * @Description 此处用于查询mes单生产出库物料，并找到它们上次生产的操作员工
	 * @param mesId, ioType
	 * @return
	 * Create at: 2021-08-12 15:37:49
	 * @author: xzs
	 * Revision:
	 *    2021-08-12 15:37:49 - first revision by xzs
	 *
	 */
	@Select("SELECT material_id FROM stk_io_bill_entry WHERE mid in (SELECT id FROM stk_io_bill WHERE source_id=#{mesId} AND stock_io_type=#{ioType})")
	public List<String> getMaterialIds(@Param("mesId") String mesId, @Param("ioType") String ioType);
	
	/**
	 * @Description 根据物料id和出入库状态查询出入库单
	 * @param materialIdList
	 * @param ioType
	 * @return
	 * Create at: 2021-08-14 11:40:20
	 * @author: xzs
	 * Revision:
	 *    2021-08-14 11:40:20 - first revision by xzs
	 *
	 */
	public List<StkIoBill> selectBatchByMaterialIdList(@Param("materialIdList") List<String> materialIdList, @Param("ioType") String ioType);
}
